package com.czkevin.cifjiaocheng;

import com.czkevin.cifjiaocheng.cef.ShitTNTEntity;
import de.tr7zw.nbtapi.NBTCompound;
import com.czkevin.cef.CustomEntityRegister;
import com.czkevin.cif.BaseBlock;
import com.czkevin.cif.CIFItemInstance;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;

public class ShitTNT extends BaseBlock {
    @Override
    public String getDisplayName(NBTCompound nbtCompound) {
        return "\u00a7c\u00a7l恶臭TNT";
    }

    @Override
    public Material getModelMaterial() {
        return Material.DIAMOND;
    }

    @Override
    public int getCustomModelData(CIFItemInstance instance) {
        return 11451401;
    }

    @Override
    public void onBlockUse(CIFItemInstance instance, PlayerInteractEvent event) {
        // 只看右手
        if(event.getHand() != EquipmentSlot.HAND) return;
        // 当玩家右键点击
        ItemStack item = event.getItem();
        // 必须用打火石才能引♂爆
        if(item == null || item.getType() != Material.FLINT_AND_STEEL) return;
        // 扣除耐久度
        event.setUseItemInHand(Event.Result.ALLOW);
        ignite(event.getClickedBlock());
    }

    public void ignite(Block block) {
        // 删除方块
        block.setType(Material.AIR);
        // 生成TNT实体
        CustomEntityRegister.getInstance().spawn(
            ShitTNTEntity.TYPE,
            block.getLocation(), null
        );
    }
}
